package jgl.vajra.management.widget;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.util.Base64;
import android.view.View;

import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.ImageViewState;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;

import java.util.HashMap;
import java.util.Map;

import jgl.vajra.management.R;
import jgl.vajra.management.manager.AccountManager;
import jgl.vajra.management.manager.ImageShowManager;
import jgl.vajra.management.widget.base.BaseCommonActivity;
import jgl.vajra.management.widget.manager.RxJavaHttpManager;
import jgl.vajra.management.widget.manager.retrofit.RetrofitJsonManager;
import jgl.vajra.management.widget.retrofithelper.rxsubscriber.ProgressObserver;
import jgl.vajra.management.widget.utils.BitmapUtils;
import jgl.vajra.management.widget.utils.UIUtils;

public class BitmapImageNetActivity extends BaseCommonActivity {

    private String bucketName;
    private String fileKey;

    @Override
    public int getLayoutId() {
        return R.layout.activity_bitmap_image_net;
    }

    @Override
    public void initView() {
        super.initView();
        bucketName = getIntent().getStringExtra("bucketName");
        fileKey = getIntent().getStringExtra("fileKey");

        SubsamplingScaleImageView imageView = findViewById(R.id.image);
//        setPictureShow(image);

        ImageShowManager.getInstance().setPictureShow(this,imageView,bucketName,fileKey);

    }

    public void setPictureShow(SubsamplingScaleImageView imageView) {


        Map<String, Object> maps = new HashMap<>();
        maps.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        maps.put("bucketName", bucketName);
        maps.put("fileKey", fileKey);
        RetrofitJsonManager.getInstance().getApiService().getDetailsImageData(maps)
                .compose(RxJavaHttpManager.applyTransformer())
                .subscribe(new ProgressObserver<String>(false, null) {
                    @Override
                    public void _onNext(String data) {

                        //将Base64编码字符串解码成Bitmap
                        byte[] decodedString = Base64.decode(data, Base64.DEFAULT);
                        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

                        float initImageScale = BitmapUtils.getInitImageScale(decodedByte, BitmapImageNetActivity.this);
                        imageView.setMaxScale(initImageScale + 2.0f);//最大显示比例
                        imageView.setImage(ImageSource.bitmap(decodedByte), new ImageViewState(initImageScale, new PointF(0, 0), 0));
                        imageView.setVisibility(View.VISIBLE);

                    }
                });
    }
}
